//
//  YWSubscribeListPageViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/9.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSubscribeListPageViewController.h"
#import "YWSquareModel.h"
#import "YWSquareDetailTableViewCell.h"
#import "AlbumDetailViewController.h"
#import "CBStoreHouseRefreshControl.h"
@interface YWSubscribeListPageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *squareDetailArray;

@property (nonatomic, retain) UITableView *squareDetailTableView;

@property (nonatomic, assign) NSInteger pageId;

@property (nonatomic, assign) NSInteger maxPageId;

@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;

@end

@implementation YWSubscribeListPageViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    _passVCNumBlock(_vcNum);
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    self.squareDetailArray = [NSMutableArray array];
    _pageId = 1;
    [self getDataByPageId:_pageId];
}

- (void)getView
{
    self.squareDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 35) style:UITableViewStylePlain];
    _squareDetailTableView.delegate = self;
    _squareDetailTableView.dataSource = self;
    _squareDetailTableView.separatorStyle = NO;
    [self.view addSubview:_squareDetailTableView];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50 * FITHEIGHT)];
    _squareDetailTableView.tableFooterView = footView;
    
    [_squareDetailTableView registerClass:[YWSquareDetailTableViewCell class] forCellReuseIdentifier:@"squareCell"];
    [_squareDetailTableView registerClass:[YWSquareDetailTableViewCell class] forCellReuseIdentifier:@"colorNumCell"];
    
//    _squareDetailTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//    }];
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.squareDetailTableView target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor blackColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
    
    _squareDetailTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageId++;
        if (_pageId > _maxPageId) {
            [_squareDetailTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByPageId:_pageId];
        }
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:0];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl
{
    [_squareDetailArray removeAllObjects];
    [_squareDetailTableView reloadData];
    _pageId = 1;
    [self getDataByPageId:_pageId];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _squareDetailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWSquareModel *model = [_squareDetailArray objectAtIndex:indexPath.row];
    if (indexPath.row < 4) {
        YWSquareDetailTableViewCell *squareCell = [tableView dequeueReusableCellWithIdentifier:@"colorNumCell"];
        squareCell.squareModel = model;
        squareCell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        if (indexPath.row == 0) {
            squareCell.numberLabel.textColor = [UIColor redColor];
        }
        else if (indexPath.row == 1)
        {
            squareCell.numberLabel.textColor = [UIColor orangeColor];
        }
        else if (indexPath.row == 2)
        {
            squareCell.numberLabel.textColor = [UIColor greenColor];
        }
        return squareCell;
    }
    YWSquareDetailTableViewCell *squareCell = [tableView dequeueReusableCellWithIdentifier:@"squareCell"];
    squareCell.squareModel = model;
    squareCell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return squareCell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];
    YWSquareModel *squareModel = [_squareDetailArray objectAtIndex:indexPath.row];
    album.albmId = squareModel.albumId;
    album.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:album animated:YES];
}

- (void)getDataByPageId:(NSInteger)pageId
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/album?device=android&key=%@&pageId=%ld&pageSize=20&statEvent=pageview%%2Franklist", _urlKey, pageId];
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _maxPageId = [[result objectForKey:@"maxPageId"] integerValue];
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWSquareModel *squareModel = [[YWSquareModel alloc] initWithDic:dic];
            [_squareDetailArray addObject:squareModel];
        }
        [_squareDetailTableView reloadData];
        [_storeHouseRefreshControl finishingLoading];
        [_squareDetailTableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
